package cn.ycc1.boot3distribution.controller;

import cn.ycc1.boot3distribution.service.DistributionListDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author ycc
 * @date 2023/12/10
 */
@RestController
@Slf4j
public class TestController {

    @GetMapping("/test2")
    public String test2() {
        List<DistributionListDto> list = new ArrayList<>();
        list.add(new DistributionListDto(1L, 10000, 0, "区域代理", "AGENT", 10000));
        list.add(new DistributionListDto(2L, 10001, 10000, "服务中心", "SERVICE", 10000));
        list.add(new DistributionListDto(3L, 10002, 10000, "服务中心", "SERVICE", 10000));
        list.add(new DistributionListDto(4L, 10003, 10001, "老板", "BOSS", 10000));
        list.add(new DistributionListDto(5L, 10004, 10003, "老板", "BOSS", 10000));
        list.add(new DistributionListDto(6L, 10005, 10004, "普通会员", "MEMBER", 10000));
        list.add(new DistributionListDto(7L, 10006, 10004, "普通会员", "MEMBER", 10000));
        list.add(new DistributionListDto(8L, 10007, 10005, "普通会员", "MEMBER", 10000));

        /**
         * 第2层数据
         */
        List<Integer> list2 = new ArrayList<>();
        List<Integer> parentArr = new ArrayList<>();
        parentArr.add(10000);
        for(DistributionListDto item : list) {
            if(parentArr.contains(item.getParentId())) {
                list2.add(item.getUserId());
            }
        }

        /**
         * 第3层数据
         */
        List<Integer> list3 = new ArrayList<>();
        for(DistributionListDto item : list) {
            if(list2.contains(item.getParentId())) {
                list3.add(item.getUserId());
            }
        }

        System.out.println(list2);
        System.out.println(list3);
        list2.addAll(list3);
        System.out.println(list2);
        // 冻结的groupId(与冻结的用户id) 关联订单id, 且是未解冻的记录，进行解冻操作。
        return "hello test2";
    }

//    public static boolean isElementInArray(int[] array, int target) {
//        Arrays.sort(array); // 确保数组已排序，因为binarySearch()方法要求输入数组已排序
//        int index = Arrays.binarySearch(array, target);
//        return index >= 0;
//    }

    @GetMapping("/test")
    public String test() {
        List<DistributionListDto> list = new ArrayList<>();
        list.add(new DistributionListDto(1L, 10000, 0, "区域代理", "AGENT", 10000));
        list.add(new DistributionListDto(2L, 10001, 10000, "服务中心", "SERVICE", 10000));
        list.add(new DistributionListDto(3L, 10002, 10000, "服务中心", "SERVICE", 10000));
        list.add(new DistributionListDto(4L, 10003, 10001, "老板", "BOSS", 10000));
        list.add(new DistributionListDto(5L, 10004, 10003, "老板", "BOSS", 10000));
        list.add(new DistributionListDto(6L, 10005, 10004, "普通会员", "MEMBER", 10000));
        list.add(new DistributionListDto(7L, 10006, 10004, "普通会员", "MEMBER", 10000));
        list.add(new DistributionListDto(8L, 10007, 10005, "普通会员", "MEMBER", 10000));

        int index = 7;
        List<DistributionListDto> list2 = new ArrayList<>();
        list2.add(list.get(index));
        System.out.println(list.get(index));

        while (list.get(index).getParentId() != 0) {
            int parentIndex = this.getParentIndex(list.get(index).getParentId(), list);
            list2.add(list.get(parentIndex));
            index = parentIndex;
            System.out.println(parentIndex);
//            System.out.println(list2);
        }

        for(DistributionListDto item : list2) {
            System.out.println(item);
        }
//        System.out.println(list);
//        for(DistributionListDto item : list) {
//            System.out.println(item);
//        }

        /**
         * 1. 直推
         * 2. 见点
         * 2.1 手益
         * 2.2 冻结
         * 3. 平级
         * 4. 会员等级处理
         * 5. 解冻
         */
        return "hello test";
    }

    private int getParentIndex(Integer parentId, List<DistributionListDto> list) {
        for(int i = 0; i < list.size(); i++) {
            if(parentId.equals(list.get(i).getUserId())) {
                System.out.println(list.get(i).getUserId());
                return i;
            }
        }
        return 0;
    }
}
